package com.app.service.impl;

import cn.hutool.core.stream.StreamUtil;
import cn.hutool.core.util.StrUtil;
import com.app.common.Result;
import com.app.model.ClassificationSubclass;
import com.app.mapper.ClassificationSubclassMapper;
import com.app.service.IClassificationSubclassService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 团购分类子类表 服务实现类
 * </p>
 *
 * @author shaokuiling
 * @since 2024-09-24
 */
@Service
public class ClassificationSubclassServiceImpl extends ServiceImpl<ClassificationSubclassMapper, ClassificationSubclass> implements IClassificationSubclassService {

    @Autowired
    private ClassificationSubclassMapper classificationSubclassMapper;

    /**
     * 管理后台————新增
     * @param classificationSubclass
     * @return
     */
    @Override
    public Result addAndUpdate(ClassificationSubclass classificationSubclass) {
        if(classificationSubclass.getId()==null){//新增

            //参数校验
            if (classificationSubclass == null ||classificationSubclass.getCategoryId()==null ||classificationSubclass.getCategoryId()<=0
                    ||classificationSubclass.getNumber()<=0 || StrUtil.isBlank(classificationSubclass.getUnit())
                    ||(classificationSubclass.getPrice() == null || classificationSubclass.getPrice().compareTo(BigDecimal.ZERO) < 0)) {
                return Result.error(400,"参数错误");
            }

            classificationSubclass.setCreateTime(LocalDateTime.now());
            if(classificationSubclass.getSort()==null){
                classificationSubclass.setSort(0);
            }
            classificationSubclass.setStatus(1);
            this.save(classificationSubclass);
            return Result.success("操作成功");
        }else if(classificationSubclass.getId()>=0){
            classificationSubclass.setUpdateTime(LocalDateTime.now());
            this.updateById(classificationSubclass);
            return Result.success("操作成功");
        }
        return Result.error(400,"非法参数，不予操作");
    }

    /**
     * 管理后台————删除
     * @param id
     * @return
     */
    @Override
    public Result daleteById(Integer id) {
        ClassificationSubclass classificationSubclass = new ClassificationSubclass();
        classificationSubclass.setId(id);
        classificationSubclass.setUpdateTime(LocalDateTime.now());
        classificationSubclass.setStatus(2);
        this.updateById(classificationSubclass);
        return Result.success("删除成功");
    }

    @Override
    public List<ClassificationSubclass> listBycategoryIds(List<Integer> idList) {
        List<ClassificationSubclass> classificationSubclasses = classificationSubclassMapper.selectList(new QueryWrapper<ClassificationSubclass>()
                .in("category_id", idList)
                .eq("status", 1)
                .orderByDesc("sort")
                .orderByDesc("create_time"));
        return classificationSubclasses;
    }
}
